import TableLyout from "@/components/commonPublic/tableLyout.vue";
import publicForm from "@/components/materialDataManage/fromCom.vue"; //模糊查询
import TableData from "@/components/public/el-table"; //主页面列表
import formJs from "./regexDetail.js"; //新增弹框配置项
import actions from "@/actions";
import API from '@/utils/commonRequest.js';
import messageBox from "@/components/public/messageBox"; //主页面分页

import {
    getSteelProtectionTestTaskLists, //主页面列表接口
    copySteelProtectionTestTask,// 复制
    submitSteelProtectionTestTaskByAuthor, //提交
    deleteSteelProtectionTestTask,//删除
} from "@/api/request.js";
export default {
    name: "rebarDetectionTask",
    components: {
        TableLyout,
        publicForm,
        TableData,
        messageBox
    },
    data() {
        return {
            name: "rebarDetectionTask", //按钮配置页面代号
            searchObj: formJs.searchObj, //搜索条件
            queryCriteriaData: {}, //分页
            userNature: null, //用户Nature
            // 弹框
            showMsgBox: false,
            msgTip: "请先选中数据行！",
            msgType: 1,
            titelName: null,
            // 弹框
            tendersObj: [],//标段list
            projectsObj: [], //项目名
            // 列表
            statusNum: null, //点击那个按钮
            isLock: false,
            currentRow: null, //列表详情
            btnObj: formJs.btnObj, //按钮
            table_config: formJs.table_config,
            tableData: [], //列表数据列表
            // checked: true, //查看下级按钮
            loading: false, //列表数据加载loading
            getList: {
                fprojectid: "",
                fCompanyid: "", // 左侧选中组织机构id
                startDates: "", // 开始时间
                endDates: "", // 截至时间
                modelType: "3",
                page: 1, // 当前页码
                pageSize: 15, // 每页显示条数
                userId: "",
                timeDates: [],
            }, // 列表给后端传参
            // 列表
            isShow: false, //是否显示弹框
            userInfo: {}, //登录用户
        };
    },

    mounted() {
        API.userNature().then((res) => {
            if (res.errcode == 0) {
                this.userNature = res.data.Nature;
            }
        }).catch((err) => {
            console.log("API", err)
        })
        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.projectsObj = data;
            this.userInfo = prev.currentUser
        }, true);
        this.getDate();
        this.table_config.operation.cols = [
            {
                prop: "fStatus",
                type: "primary",
                handleRow: this.handleRow,
            },
        ];

        this.table_config.operation["specialClass"] = function (rowData) {
            var color = null;
            if (rowData == "待检测") {
                color = "red !important";
            } else if (rowData == "检测中") {
                color = "orange !important";
            } else if (rowData == "已完成") {
                color = "green !important";
            } else {
                color = "#409eff !important";
            }
            return color;
        };
    },
    methods: {

        // 弹框
        confirmMsg() {
            this.showMsgBox = false;
        },
        // 带功能确定
        confirmMsgs() {
            this.showMsgBox = false;
            if (this.titelName == "复制") {
                this.yj_copy(this.currentRow);
            }
            if (this.titelName == "提交") {
                this.yj_submit(this.currentRow);
            }
            if (this.titelName == "删除") {
                this.yj_delete(this.currentRow);
            }
        },
        // 弹框

        // 重置
        reset(e) {
            this.getList = this.$options.data.call(this).getList;
            this.getList.fprojectid = e;
        },
        getBtnName(name, status) {
            this.statusNum = status
            this.yj_edit(status);
        },
        // 新增/编辑
        yj_edit(num) {
            var data;
            if (num == 7) { //查看
                if (this.currentRow.fId != null) {
                    this.$router.push({
                        path: "/rebarThicknessManage/rebarDetectionTaskText",
                    });
                    data = {
                        data: this.currentRow,
                        fCompanyid: this.getList.fCompanyid,
                        status: num,
                        getList: this.getList,
                        userNature: this.userNature
                    }
                } else {
                    this.$message.warning({
                        content: "请先选择需要查看的数据",
                        duration: 1000
                    });

                }

            } else {
                if (this.userNature == '施工单位' || this.userNature == '总监办' || this.userNature == "中心试验室") {
                    if (num == 1) { // 新增
                        if (this.getList.fCompanyid != "" && this.getList.fCompanyid != null && this.getList.fprojectid != "" && this.getList.fprojectid != null) {
                            this.$router.push({
                                path: "/rebarThicknessManage/rebarDetectionTaskText",
                            });
                            data = {
                                data: this.currentRow,
                                fCompanyid: this.getList.fCompanyid,
                                status: num,
                                getList: this.getList,
                                userNature: this.userNature
                            }
                        } else {
                            this.$message.warning({
                                content: '请先选择项目名称和标段名称',
                                duration: 1000
                            })
                        }
                    } else if (num == 2) { //复制
                        if (this.currentRow.fId != null && (this.currentRow.fStatus == "编辑中" || this.currentRow.fStatus == "被撤回")) {
                            this.msgTip = "是否继续复制当前数据?";
                            this.msgType = 2; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
                            this.showMsgBox = true;
                            this.titelName = "复制";
                        } else {
                            this.$message.warning({
                                content: "请先选择需要复制的数据,且检测状态为'编辑中'或'被撤回'",
                                duration: 1000
                            });
                        }
                    } else if (num == 3) { //编辑
                        if (this.currentRow.fId != null && (this.currentRow.fStatus == "编辑中" || this.currentRow.fStatus == "被撤回")) {
                            this.$router.push({
                                path: "/rebarThicknessManage/rebarDetectionTaskText",
                            });
                            data = {
                                data: this.currentRow,
                                fCompanyid: this.getList.fCompanyid,
                                getList: this.getList,
                                status: num,
                                userNature: this.userNature
                            }
                        } else {
                            this.$message.warning({
                                content: "请先选择需要编辑的数据，且检测状态为'编辑中'或'被撤回'",
                                duration: 1000
                            });
                        }

                    } else if (num == 4) { //提交
                        if (this.currentRow.fId != null && (this.currentRow.fStatus == "编辑中" || this.currentRow.fStatus == "被撤回")) {
                            if (this.currentRow.fEngineeringpart != "" && this.currentRow.fEngineeringpart != null) {
                                this.msgTip = "是否继续提交当前数据?";
                                this.msgType = 2; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
                                this.showMsgBox = true;
                                this.titelName = "提交";
                            } else {
                                this.$message.warning({
                                    content: "请先完善当前数据工程部位填写！",
                                    duration: 1000
                                });
                            }
                        } else {
                            this.$message.warning({
                                content: "请先选择需要提交的数据,且检测状态为'编辑中'或'被撤回'",
                                duration: 1000
                            });
                        }
                    } else if (num == 5) { //删除
                        if ((this.currentRow.fId != null && this.currentRow.fStatus == "编辑中") || (this.currentRow.fId != null && this.currentRow.fStatus == "被撤回")) {
                            this.msgTip = "是否继续删除当前数据?";
                            this.msgType = 2; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
                            this.showMsgBox = true;
                            this.titelName = "删除";
                        } else {
                            this.$message.warning({
                                content: "请先选择需要删除的数据，且检测状态为'编辑中'",
                                duration: 1000
                            });

                        }
                    }
                } else {
                    this.$message.warning({
                        content: '该用户无权限',
                        duration: 1000
                    })
                }
            }
            sessionStorage.setItem(
                "GxEmbedded_primaryInfo",
                JSON.stringify(data)
            );
        },
        // 获取列表数据
        yj_searchForm(data) {
            window.sessionStorage.setItem(
                "GxEmbedded-rebarThicknessManage_getList",
                JSON.stringify(data)
            );
            var id;
            if (data.fCompanyid != "") {
                id = data.fCompanyid;
            } else {
                id = data.fprojectid;
            }
            var obj = {
                fCompanyid: id, // 标段ID
                startDates: data.startDates, //进场开始时间
                endDates: data.endDates, //进场结束时间
                modelType: "3",
                page: data.page, //当前页码
                pageSize: data.pageSize, //每页显示条数
                userId: this.userInfo.id, //查询待办或已办时传 列表页查询不传
            };
            this.loading = true;
            var that = this;
            getSteelProtectionTestTaskLists(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        this.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    console.log("列表数据 接口出错" + error.msg);
                    this.loading = false;
                });
        },
        // 查看
        yj_viewForm(row, label) {
            this.currentRow = row;
            if (label == "任务编号") {
                this.yj_edit(7)
            }
        },

        // 点击当前行
        rowChange(val) {
            this.currentRow = val;
            return val;
        },

        // 复制
        yj_copy(data) {
            var obj = {
                fId: data.fId,
                userId: this.userInfo.id, //当前登录人id
                userName: this.userInfo.userName //当前登录人姓名
            }
            this.loading = true;
            copySteelProtectionTestTask(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.yj_searchForm(this.getList)
                        this.$message.success({
                            content: "复制成功",
                            duration: 1000
                        })
                    } else {
                        this.loading = false;


                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        })
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("复制接口" + error.msg);
                });
        },
        // 提交
        yj_submit(row) {
            this.isLock = true
            this.loading = true;
            submitSteelProtectionTestTaskByAuthor({
                fId: row.fId,
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.isLock = false;
                        this.yj_searchForm(this.getList);
                        this.$message.success({
                            content: "提交成功",
                            duration: 1000
                        })
                    } else {
                        this.isLock = false;
                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        })
                        this.loading = false;
                        return false;
                    }
                })
                .catch((err) => {
                    this.loading = false;
                    console.log("提交接口" + err.msg);
                });
        },
        // 删除
        yj_delete(row) {
            console.log(3333);
            this.isLock = true
            this.loading = true;
            deleteSteelProtectionTestTask({
                fId: row.fId,
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.isLock = false;
                        this.yj_searchForm(this.getList);
                        this.$message.success({
                            content: "删除成功",
                            duration: 1000
                        })
                    } else {
                        this.isLock = false;
                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        })
                        this.loading = false;
                        return false;
                    }
                })
                .catch((err) => {
                    this.loading = false;
                    console.log("删除接口" + err.msg);
                });
        },


        //获取时间
        getDate() {
            if (JSON.parse(sessionStorage.getItem("GxEmbedded-rebarThicknessManage_getList"))) {

                var dataList = JSON.parse(
                    sessionStorage.getItem("GxEmbedded-rebarThicknessManage_getList")
                );
                if (dataList.fprojectid != "") {
                    this.getTender(dataList.fprojectid);
                } else {
                    data.fprojectid = this.projectsObj[1].fItemValue;
                    this.getTender(dataList.fprojectid);
                }
                if (dataList.startDates == "" || dataList.startDates == undefined || dataList.startDates == null || dataList.endDates == "" || dataList.endDates == undefined || dataList.endDates == null) {
                    dataList.startDates = this.common.getDay(-90); // 开始时间
                    dataList.endDates = this.common.getDay(0); // 截至时间
                }
                this.getList = {
                    fprojectid: dataList.fprojectid,
                    fCompanyid: dataList.fCompanyid, // 左侧选中组织机构id
                    startDates: dataList.startDates, // 开始时间
                    endDates: dataList.endDates, // 截至时间
                    modelType: "3", // 0待办 1已办 2查询
                    page: 1, // 当前页码
                    pageSize: 15, // 每页显示条数
                    userId: this.userInfo.id,
                    timeDates: [dataList.startDates, dataList.endDates],
                };
            } else {
                this.getList.startDates = this.common.getDay(-90); // 开始时间
                this.getList.endDates = this.common.getDay(0); // 截至时间
                this.getList.timeDates = [
                    this.getList.startDates,
                    this.getList.endDates,
                ];

            }
        },




        // 筛选条件
        async getTender(e) {
            await this.$http(
                " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
                e, {
                headers: {
                    "Content-Type": "application/json",
                    Authorization: localStorage.getItem("access_token"),
                },
            }
            ).then((res) => {
                if (res.status == 200) {
                    var data = res.data.data.GXCompanyList;
                    this.tendersObj = [];
                    if (data.length == 0) {
                        this.$message.warning({
                            content: "有其他人员登录此账号！",
                            duration: 1000,
                        });
                        setTimeout(() => {
                            window.location.href = "http://172.18.5.201:1080/login";
                        }, 1200);
                    } else  if (data.length == 1) {
                        data.map((item) => {
                            this.tendersObj.push({
                                fItemName: item.sectionName,
                                fItemValue: item.sectionId,
                            });
                        });
                        this.getList.fCompanyid = data[0].sectionId;
                    } else {
                        this.tendersObj = [{
                            fItemName: "所有标段",
                            fItemValue: "",
                        },];
                        data.map((item) => {
                            this.tendersObj.push({
                                fItemName: item.sectionName,
                                fItemValue: item.sectionId,
                            });
                        });
                    }
                }
            });
        },
        selectTime(e) {
            if (e[0] != "" && e[1] != "") {
                this.getList.startDates = e[0] + " " + "00:00:00"; // 开始时间
                this.getList.endDates = e[1] + " " + "23:59:59"; // 截至时间
            } else {
                this.getList.startDates = ""; // 开始时间
                this.getList.endDates = ""; // 截至时间
            }
            this.getList.timeDates = [e[0], e[1]];
        },
        selectCompanyid(e, index, prop) {
            console.log("selectCompanyid", e, index, prop);
        },
        // 筛选条件
    },
};